#!/bin/bash -e

pid=$1

CLK=$(getconf CLK_TCK)
path=/proc/${pid}/stat

echo $path
echo "CLK: ${CLK}"

# utime + stime
#old=$(awk '{printf "%lu", $14 + $15 + $43}' ${path})
old=$(awk '{printf "%lu", $14 + $15}' ${path})
g_old=$(awk '{print $43}' ${path})
while true
do
        now=$(awk '{printf "%lu", $14 + $15}' ${path})
        #now=$(awk '{printf "%lu", $14 + $15 + $43}' ${path})
        g_now=$(awk '{print $43}' ${path})

        delta=$(echo "$now - $old" | bc)
        usage=$(echo "scale=2; $delta * 100 / ${CLK}" | bc)

        gd=$(echo "${g_now} - ${g_old}" | bc)
        g_usage=$(echo "scale=2; $gd * 100 / ${CLK}" | bc)
        echo "CPU usage: ${usage}%, guest: ${g_usage}"
        old=$now
        g_old=${g_now}

        sleep 1
done

